package top.huxy.boot.config;

import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.huxy.qqbot.acks.Ack;
import top.huxy.qqbot.acks.Login;
import top.huxy.qqbot.core.roundrobin.MessageTrigger;
import top.huxy.qqbot.core.roundrobin.RoundRobinEventHandler;
import top.huxy.qqbot.framework.NodeQQSession;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

@Configuration
@ConditionalOnProperty(name = "huxy.app.qq-bot.enable", havingValue = "true")
@ConfigurationProperties(prefix = "huxy.app.qq-bot")
@Data
public class QQBotConfig {
    private String host;
    private int port;
    private long qq;
    private String password;
    private String verification;

    @Bean
    public NodeQQSession nodeQQSession(ApplicationContext context) {
        Map<String, MessageTrigger> triggerMap = context.getBeansOfType(MessageTrigger.class);
        return new NodeQQSession(host, port, qq, password, new MyQQBotHandler(triggerMap.values()), verification);
    }

    private static class MyQQBotHandler extends RoundRobinEventHandler {
        public MyQQBotHandler(Collection<MessageTrigger> messageTriggers) {
            super(messageTriggers);
        }

        @Override
        public void onSocketInitException(Exception e, NodeQQSession session) {
            e.printStackTrace();
            super.onSocketInitException(e, session);
        }

        @Override
        public void onSocketGetInputStreamException(IOException e, NodeQQSession session) {
            e.printStackTrace();
            super.onSocketGetInputStreamException(e, session);
        }

        @Override
        public void onReadInputStreamException(IOException e, NodeQQSession session) {
            e.printStackTrace();
            super.onReadInputStreamException(e, session);
        }

        @Override
        public void onSendMessageException(IOException e, NodeQQSession session, Ack ack) {
            e.printStackTrace();
            super.onSendMessageException(e, session, ack);
        }

        @Override
        public void beforeQQLogin(long qq, String pwd) {
            NodeQQSession session = getCurrentSession();
            if(session!=null){
                session.sendMessage(Collections.singletonList(new Login(124567890L,"qwertyuiop","ppppp")));
                session.sendMessage(Collections.singletonList(new Login(124567891L,"qwertyuiop","ppppp")));
                session.sendMessage(Collections.singletonList(new Login(124567892L,"qwertyuiop","ppppp")));
                session.sendMessage(Collections.singletonList(new Login(124567893L,"qwertyuiop","ppppp")));
            }
        }

        @Override
        public void afterSendMessage(Ack ack) {

        }
    }
}
